import { actionTypes as at } from './constants';
import { UserAction, User } from './model';

export interface UsersState {
  users: User[];
  isLoading: boolean;
  isFetched: boolean;
}

export const initialState: UsersState = {
  users: [],
  isLoading: false,
  isFetched: false
};

export default (state = initialState, action: UserAction): UsersState => {
  switch (action.type) {
    case at.USERS_SAMPLE_FETCH:
      return {
        ...state,
        isLoading: true,
        isFetched: false,
        users: initialState.users,
      };
    case at.USERS_SAMPLE_FETCH_SUCCESS:
      return {
        ...state,
        isLoading: false,
        isFetched: true,
        users: action.payload,
      };
    case at.USERS_SAMPLE_FETCH_ERROR:
      return {
        ...state,
        isLoading: false,
        isFetched: false,
        users: initialState.users,
      };
    default:
      return state;
  }
};
